package com.qdxwx.data.service.praise;

import com.qdxwx.data.mapper.praise.PraiseMapper;
import com.qdxwx.data.service.article.ArticleService;
import com.qdxwx.models.praise.Praise;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * 点赞服务接口实现
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
@Service
public class PraiseServiceImpl implements PraiseService {

	@Autowired
	private PraiseMapper praiseMapper;
	@Autowired
	private ArticleService articleService;

	@Override
	public int addPraise(Praise praise) {
		//根据点赞目标 type 修改相应的 点赞总数
		int type = praise.getTypeId();
		//添加点赞记录
		praise.setCreateTime(LocalDateTime.now());
		//点赞类型为3的是文章点赞
		if (type == 3) {
			articleService.updatePraiseCount(praise.getTargetId().intValue());
		}
		//清除文章排行缓存
		return praiseMapper.addPraise(praise);
	}

	@Override
	public int countHits(Praise praise) {
		return praiseMapper.count(praise);
	}

}
